<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#">
<head>
    <meta charset='utf-8'>
    <title>Fresco | 可关闭的引用</title>
    <link rel='stylesheet' href='/static/site.css' type='text/css'/>
    <link rel="stylesheet" href='/static/pygments.css' type='text/css'/>
    <link rel='shortcut icon' href='/static/favicon.png'>
    <meta name='viewport' content='width=480'>
    <meta property="og:title" content="可关闭的引用" />
    <meta property="og:site_name" content="fresco">
    <meta property='og:description' content='Fresco is an image management library.'>
    <meta property='og:image' content='http://facebook.github.io/fresco/static/fresco-og-image.png'>
    <meta property='og:url' content="http://facebook.github.io/fresco/docs/closeable-references.html">
    
    <meta property='og:type' content='website'>
    
    <script type="text/javascript" src="//use.typekit.net/vqa1hcx.js"></script>
    <script type="text/javascript">{'try{Typekit.load();}catch(e){}'}</script>
    <!-- Google Analytics -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-43024238-8', 'auto');
      ga('send', 'pageview');

    </script>
</head>
<body>

<header class='topbar'><nav class='width'>
<a href='/'>
  <img src="/static/fresco-logo.png" class="logo">
</a>
    <ul>
      <li><a href="/docs/index.html#_"
          
          class="active" >
          文档
      </a></li>
      <li><a href="/"
          >
          关于
      </a></li>
      <li><a href="/support.html"
          >
          支持
      </a></li>
      <li><a href="/javadoc">
          API
      </a></li>
      <li><a href="http://github.com/facebook/fresco">GitHub</a>
      <li><a href="/about.html"
          >
      关于本文档</a></li>
    </ul>
</nav></header>


<section class='content'><div class='width'>

<nav class='toc'>
  
    <section>
      <h3>配置和使用</h3>
      <ul>
        
          <li>
            <a href="/docs/download-fresco.html#_">
              下载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/index.html#_">
              配置和开始使用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/concepts.html#_">
              关键概念
            </a>
            
          </li>
        
          <li>
            <a href="/docs/supported-uris.html#_">
              支持的 URIs
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Drawee 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/using-drawees-xml.html#_">
              在XML中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-drawees-code.html#_">
              在Java代码中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/drawee-components.html#_">
              Drawee的各种效果配置
            </a>
            
          </li>
        
          <li>
            <a href="/docs/scaling.html#_">
              缩放
            </a>
            
          </li>
        
          <li>
            <a href="/docs/rounded-corners-and-circles.html#_">
              圆角/圆圈
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-controllerbuilder.html#_">
              使用ControllerBuilder
            </a>
            
          </li>
        
          <li>
            <a href="/docs/progressive-jpegs.html#_">
              渐进式JPEG图
            </a>
            
          </li>
        
          <li>
            <a href="/docs/animations.html#_">
              动画图(gif)
            </a>
            
          </li>
        
          <li>
            <a href="/docs/requesting-multiple-images.html#_">
              多图请求及图片复用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/listening-download-events.html#_">
              监听下载事件
            </a>
            
          </li>
        
          <li>
            <a href="/docs/resizing-rotating.html#_">
              缩放和旋转
            </a>
            
          </li>
        
          <li>
            <a href="/docs/modifying-image.html#_">
              图片的修改
            </a>
            
          </li>
        
          <li>
            <a href="/docs/image-requests.html#_">
              图片请求
            </a>
            
          </li>
        
          <li>
            <a href="/docs/writing-custom-views.html#_">
              自定义View
            </a>
            
          </li>
        
          <li>
            <a href="/docs/gotchas.html#_">
              一些陷阱
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Image Pipeline 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/intro-image-pipeline.html#_">
              什么是Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/configure-image-pipeline.html#_">
              配置Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/caching.html#_">
              缓存的行为控制
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-image-pipeline.html#_">
              直接使用Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/datasources-datasubscribers.html#_">
              数据源/订阅数据源
            </a>
            
          </li>
        
          <li>
            <a href="/docs/closeable-references.html#_" class="active">
              可关闭的引用
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>第三方类库</h3>
      <ul>
        
          <li>
            <a href="/docs/using-other-network-layers.html#_">
              自定义网络加载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-other-image-loaders.html#_">
              使用其他的Image Loader
            </a>
            
          </li>
        
      </ul>
    </section>
  
</nav>


<article class='withtoc'>
    <h1>
      可关闭的引用
      <a class="edit-page-link" href="https://github.com/liaohuqiu/fresco-docs-cn/blob/docs/docs/02-closeable-references.md" target="_blank">编辑和纠错</a>
    </h1>
    <p></p>

    <p><strong>本页内容仅为高级使用作参考</strong> </p>

<p>大部分的应用，直接使用<a href="using-drawees-xml.html">Drawees</a>就好了，不用考虑关闭的事情了。</p>

<p>Java带有垃圾收集功能，许多开发者习惯于不自觉地创建一大堆乱七八糟的对象，并且想当然地认为他们会从内存中想当然地消失。</p>

<p>在5.0系统之前，这样的做法对于操作Bitmap是极其糟糕的。Bitmap占用了大量的内存，大量的内存申请和释放引发频繁的GC，使得界面卡顿不已。</p>

<p>Bitmap 是Java中为数不多的能让Java开发者想念或者羡慕C++以及C++众多的指针库，比如<a href="http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/smart_ptr.htm">Boost</a> 的东西。</p>

<p>Fresco的解决方案是: <a href="../javadoc/reference/com/facebook/common/references/CloseableReference.html">可关闭的引用(CloseableReference)</a> </p>

<p>为了正确地使用它，请按以下步骤进行操作:</p>

<h4>1. 调用者拥有这个引用</h4>

<p>我们创建一个引用，但我们传递给了一个调用者，调用者将持有这个引用。</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">CloseableReference</span><span class="o">&lt;</span><span class="n">Val</span><span class="o">&gt;</span> <span class="nf">foo</span><span class="o">()</span> <span class="o">{</span>
  <span class="n">Val</span> <span class="n">val</span><span class="o">;</span>
  <span class="k">return</span> <span class="n">CloseableReference</span><span class="o">.</span><span class="na">of</span><span class="o">(</span><span class="n">val</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div>
<h4>2. 持有者在离开作用域之前，需要关闭引用</h4>

<p>创建了一个引用，但是没有传递给其他调用者，在结束时，需要关闭。</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kt">void</span> <span class="nf">gee</span><span class="o">()</span> <span class="o">{</span>
  <span class="n">CloseableReference</span><span class="o">&lt;</span><span class="n">Val</span><span class="o">&gt;</span> <span class="n">ref</span> <span class="o">=</span> <span class="n">foo</span><span class="o">();</span>
  <span class="k">try</span> <span class="o">{</span>
    <span class="n">haa</span><span class="o">(</span><span class="n">ref</span><span class="o">);</span>
  <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
    <span class="n">ref</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
  <span class="o">}</span>
<span class="o">}</span>
</code></pre></div>
<p><code>finally</code> 中最适合做此类事情了。</p>

<h4>3. 除了引用的持有者，闲杂人等<strong>不得</strong>关闭引用</h4>

<p>作为一个参数传递，调用者持有这个引用，在下面的函数体中，不能关闭引用。</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kt">void</span> <span class="nf">haa</span><span class="o">(</span><span class="n">CloseableReference</span><span class="o">&lt;?&gt;</span> <span class="n">ref</span><span class="o">)</span> <span class="o">{</span>
  <span class="n">Log</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Haa: &quot;</span> <span class="o">+</span> <span class="n">ref</span><span class="o">.</span><span class="na">get</span><span class="o">());</span>
<span class="o">}</span>
</code></pre></div>
<p>如果调用了 <code>.close()</code>, 调用者尝试调用 <code>.get()</code>时，会抛出<code>IllegalStateException</code></p>

<h4>4. 在赋值给变量前，先进行clone</h4>

<p>在类中使用:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">class</span> <span class="nc">MyClass</span> <span class="o">{</span>
  <span class="n">CloseableReference</span><span class="o">&lt;</span><span class="n">Val</span><span class="o">&gt;</span> <span class="n">myValRef</span><span class="o">;</span>

  <span class="kt">void</span> <span class="nf">mmm</span><span class="o">(</span><span class="n">CloseableReference</span><span class="o">&lt;</span><span class="n">Val</span><span class="o">&gt;</span> <span class="n">ref</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">myValRef</span> <span class="o">=</span> <span class="n">ref</span><span class="o">.</span><span class="na">clone</span><span class="o">();</span>
  <span class="o">};</span>
  <span class="c1">// caller can now safely close its copy as we made our own clone.</span>

  <span class="kt">void</span> <span class="nf">close</span><span class="o">()</span> <span class="o">{</span>
    <span class="n">CloseableReference</span><span class="o">.</span><span class="na">closeSafely</span><span class="o">(</span><span class="n">myValRef</span><span class="o">);</span>
  <span class="o">}</span>
<span class="o">}</span>
<span class="c1">// MyClass的调用者需要关闭myValRef</span>
</code></pre></div>
<p>在内部中使用:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kt">void</span> <span class="nf">haa</span><span class="o">(</span><span class="n">CloseableReference</span><span class="o">&lt;?&gt;</span> <span class="n">ref</span><span class="o">)</span> <span class="o">{</span>
  <span class="kd">final</span> <span class="n">CloseableReference</span><span class="o">&lt;?&gt;</span> <span class="n">refClone</span> <span class="o">=</span> <span class="n">ref</span><span class="o">.</span><span class="na">clone</span><span class="o">();</span>
  <span class="n">executor</span><span class="o">.</span><span class="na">submit</span><span class="o">(</span><span class="k">new</span> <span class="nf">Runnable</span><span class="o">()</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">try</span> <span class="o">{</span>
        <span class="n">Log</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Haa Async: &quot;</span> <span class="o">+</span> <span class="n">refClone</span><span class="o">.</span><span class="na">get</span><span class="o">());</span>
      <span class="o">}</span> <span class="k">finally</span> <span class="o">{</span>
        <span class="n">refClone</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
      <span class="o">}</span>
    <span class="o">}</span>
  <span class="o">});</span>
  <span class="c1">// 当前函数域内可安全关闭，闭包内为已经clone过的引用。</span>
<span class="o">}</span>
</code></pre></div>

    <div class="docs-prevnext">
      
        <a href="/docs/datasources-datasubscribers.html#_">&larr; 上一页</a>
      
      
        <a class="right" href="/docs/using-other-network-layers.html#_">下一页 &rarr;</a>
      
    </div>

    <a id="_"></a>
</article>

</div></section>
<footer><div class='width'>
    &copy; Copyright 2015, Facebook Inc.
</div></footer>

<script src="/static/linkify.js"></script>
<!-- Google Analytics -->
</body>
</html>

